#!/bin/bash
#
#******************************************************************************
# bootstrap (XPC)
#------------------------------------------------------------------------------
##
# \file       	bootstrap
# \library    	XPC
# \author     	Chris Ahlstrom
# \date       	2008-03-02
# \update     	2014-06-28
# \version    	$Revision$
# \license    	$XPC_SUITE_GPL_LICENSE$
#
#     The above is modified by the following to remove even the mild GPL
#     restrictions:
#
#     Use this script in any manner whatsoever.  You
#     don't even need to give me any credit.
#
#     However, keep in mind the value of the GPL in keeping software and its
#     descendant modifications available to the community for all time.
#
# Autoconf bootstrap script for the XPC library suite:
#
#     Bash, not sh, is required; some systems do not symlink sh to bash.
#
#     This file creates the proper environment for development.  It also
#     runs the configure script by default.
#
#     Run "./bootstrap --help" for options.
#
#     Run "./bootstrap --make" to rebuild the configuration, make the
#     libraries and executables, make the documentation, and make the tags
#     files for emacs and vi editing.
#
#     This file is the Bootstrap file for the automake setup of the XPC
#     directory.  The following subnotes apply.
#
# aclocal -I config:
#
#     Generates aclocal.m4 (by scanning configure.ac).  The following option
#     applies:
#
#        -I config      Adds directory "config" to the search list for m4 files.
#
# libtoolize --force --copy:
#
#     Allows the building of the project using libtool.  --force causes the
#     libtools files to always be overwritten. --copy means copy the libtool
#     files instead of symlinking to them.
#
# autoheader:
#
#     Generates configuration headers.
#
# automake --foreign --add-missing --copy:
#
#        --foreign      Relaxes GNU standards for the files that
#                       should be present in a GNU distribution.
#
#        --add-missing  Creates the skeleton files that document an
#                       GNU project.
#
#        --copy         Copy the skeleton files, rather than making a
#                       symlink.
#
# autoconf:
#
#     This call reads configure.in and uses it to create the rest of the stuff
#     needed to build the project.
#
# gettextize (now known as 'autopoint'):
#
#     This call prepares the project for the usage of gettext features.
#     It is not completely used in the XPC project (at this time), but is
#     supported.  The functionality is now in the bootstrap-po script.
#
#     Note that autopoint requires CVS to be installed.
#     Need automake-1.8 or above to guarantee some new macros.
#
# gcc versions:
#
#     Very old compilers (e.g. gcc 2.95.4) are not supported by this project.
#     You will want to upgrade your compiler.
#
# Purpose:
#
#     This script is meant to be used by a developer to setup this project
#     from scratch, and generated and run a GNU automake configure script.
#
#     Run the command "./bootstrap --help" for more information.
#
# New functionality:
#
#     We want to be able to install the bootstrap scripts so they can be
#     used by any project.  We want to preserve the ability to run it
#     locally as well.
#
#------------------------------------------------------------------------------

#******************************************************************************
#  Provide a sane environment, just in case it is needed.
#------------------------------------------------------------------------------

LANG=C
export LANG
CYGWIN=binmode
export CYGWIN

export XPC_SCRIPT_EDIT_DATE="2014-06-28"
export XPC_LIBRARY_API_VERSION="1.1"
export XPC_LIBRARY_VERSION="$XPC_LIBRARY_API_VERSION.0"

#******************************************************************************
#  Version info
#------------------------------------------------------------------------------

if [ "$1" == "--version" ] || [ "$1" == "-v" ] ; then
   echo "XPC Basic version $XPC_LIBRARY_VERSION $XPC_SCRIPT_EDIT_DATE"
   exit 0
fi

#******************************************************************************
#  Set up for local versus system usage of bootstrapping
#------------------------------------------------------------------------------

INSTALL_PREFIX="/usr/local"
HELP_DIR="./scripts"
SCRIPTS_DIR="./scripts"

if [ "$0" != "./bootstrap" ] ; then
   INSTALL_PREFIX="${0%/bin/bootstrap}"
   HELP_DIR="$INSTALL_PREFIX/share/doc/xpc-$XPC_LIBRARY_API_VERSION"
   SCRIPTS_DIR="$INSTALL_PREFIX/bin"
fi

source $SCRIPTS_DIR/xpc_boot_functions

if [ $? != 0 ] ; then
   echo "'source $SCRIPTS_DIR/xpc_boot_functions' failed.  Must abort."
   exit 255
fi

#******************************************************************************
#  Option settings
#------------------------------------------------------------------------------

DOBACKUP="no"
DOINSTALL="no"
DOUNINSTALL="no"
DOBOOTSTRAP="yes"
DOCONFIGURE="no"
DOCLEAN="no"
DODEBCLEAN="no"
DOSUPERCLEAN="no"
DODEBUG="no"
DOINTLBOOT="no"
DOLOG="no"
DOTESTONLY="no"
DOVERSION="no"

LOGFILENAME=""
OLDCONF="no"
M4DIR="m4"

AUTOPOINT=autopoint                    # instead of gettextize

#******************************************************************************
#  Help
#------------------------------------------------------------------------------

if [ "$1" == "--help" ] ; then
   cat $HELP_DIR/bootstrap.help
   exit $EXIT_ERROR_HELP
fi

#******************************************************************************
#  Brute-force options loop
#------------------------------------------------------------------------------

if [ $# -ge 1 ] ; then

   while [ "$1" != "" ] ; do

#     echo "- Processing command-line option $1"

      case "$1" in

         --boot-install)
            DOINSTALL="yes"
            DOBOOTSTRAP="no"
            ;;

         --boot-uninstall)
            DOUNINSTALL="yes"
            DOBOOTSTRAP="no"
            ;;

         --prefix)
            shift
            INSTALL_PREFIX="$1"
            ;;

         --intl-boot | -ib | -po | --po)
            DOINTLBOOT="yes"
            ;;

         --no-bootstrap | -nb)
            DOBOOTSTRAP="no"
            ;;

         --backup)
            DOBOOTSTRAP="no"
            DOBACKUP="yes"
            ;;

         --no-configure | -nc)
            DOCONFIGURE="no"
            ;;

         --configure | -c)
            DOCONFIGURE="yes"
            ;;

         --enable-debug | --debug | -d | -ed)
            DOCONFIGURE="yes"
            DODEBUG="yes"
            ;;

         --reconstruct)
            DOBOOTSTRAP="yes"
            DOINTLBOOT="yes"
            DOCONFIGURE="yes"
            ;;

         --clean | clean)
            DOBOOTSTRAP="no"
            DOCLEAN="yes"
            ;;

         --super-clean)
            DOBOOTSTRAP="no"
            DOCLEAN="yes"
            DOSUPERCLEAN="yes"
            DOBACKUP="yes"
            ;;

         --deb-clean)
            DOBOOTSTRAP="no"
            DODEBCLEAN="yes"
            ;;

         --log)
            DOLOG="yes"
            shift
            LOGFILENAME="$1"
            if [ "$LOGFILENAME" == "" ] ; then
               die 5 "CLI" "Please specify a --log name."
            fi
            SUBSTR="${LOGFILENAME:0}"
            if [ $SUBSTR == "-" ] || [ $SUBSTR == "--" ] ; then
               die 5 "CLI" "Please specify a legal (no hyphen) --log name."
            fi
            ;;

         --version)
            DOVERSION="yes"
            DOBOOTSTRAP="no"
            ;;

         --test)
            DOTESTONLY="yes"
            ;;

         --die-test)
               die 256 "TEST" "Please specify a legal non-test option," "idiot."
            ;;

         *)
            echo "? Unsupported XPC bootstrap option; --help for more information" ;
            exit $EXIT_ERROR_NO_SUCH_OPTION
            ;;

      esac
      shift
   done
fi

checklog ;

#******************************************************************************
#  Version info
#------------------------------------------------------------------------------

if [ $DOVERSION == "yes" ] ; then
   echo "XPC Basic version $XPC_LIBRARY_VERSION $XPC_SCRIPT_EDIT_DATE"
   exit 0
fi

#******************************************************************************
#  Implement the current test option.
#------------------------------------------------------------------------------

if [ $DOTESTONLY == "yes" ] ; then

   clean_by_ext
   exit $EXIT_ERROR_TEST_ONLY

fi

#******************************************************************************
#  Just install the "boot" scripts
#------------------------------------------------------------------------------

if [ $DOINSTALL == "yes" ] ; then

   if [ ! -d scripts ] ; then
      echo "? COULD NOT FIND THE 'scripts' DIRECTORY.  Please run this script"
      echo "  from the root of XPC Basic (xpc_basic)."
      exit $EXIT_ERROR_NO_SCRIPTS_DIR
   fi

   install bootstrap $INSTALL_PREFIX/bin
   install bootstrap $INSTALL_PREFIX/bin/xpc_bootstrap
   install scripts/xpc_boot_functions $INSTALL_PREFIX/bin
   install scripts/xpc-bootstrap-po $INSTALL_PREFIX/bin
   install scripts/build $INSTALL_PREFIX/bin
   install -d $INSTALL_PREFIX/share/doc/xpc-$XPC_LIBRARY_API_VERSION
   install --mode=644 scripts/*.help $INSTALL_PREFIX/share/doc/xpc-$XPC_LIBRARY_API_VERSION
   exit $?

fi

#******************************************************************************
#  Just uninstall the "boot" scripts
#------------------------------------------------------------------------------

if [ $DOUNINSTALL == "yes" ] ; then

   # Note that we don't remove the xpc-1.1 directory here, though.

   rm -f $INSTALL_PREFIX/bin/bootstrap
   rm -f $INSTALL_PREFIX/bin/xpc_bootstrap
   rm -f $INSTALL_PREFIX/bin/xpc_boot_functions
   rm -f $INSTALL_PREFIX/bin/xpc-bootstrap-po
   rm -f $INSTALL_PREFIX/bin/build
   echo "'$INSTALL_PREFIX/share/doc/xpc-$XPC_LIBRARY_API_VERSION' will not be deleted."
   rm -f $INSTALL_PREFIX/share/doc/xpc-$XPC_LIBRARY_API_VERSION/*.help
   exit $?

fi

#******************************************************************************
#  Implement the clean option.
#------------------------------------------------------------------------------
#
#     This goes well beyond "make clean" and "make distclean".  It cleans
#     out /everything/ that gets created by bootstrapping and building the
#     library and test application.
#
#------------------------------------------------------------------------------

if [ $DOSUPERCLEAN == "yes" ] ; then

   $SCRIPTS_DIR/xpc-bootstrap-po --intl-clean

fi

if [ $DOCLEAN == "yes" ] ; then

   clean_tree

   echo "   All junk files removed (even if make says there's no rules for"
   echo "   'clean', and errors seem to have occurred)."

fi

if [ $DODEBCLEAN == "yes" ] ; then

   echo "? --deb-clean NOT READY"
   exit 1

fi

if [ $DOBOOTSTRAP == "yes" ] ; then

#************************************************************************
#  Set up GNU Autotools
#------------------------------------------------------------------------

   AUTOMAKE_BAD=no
   INSTALL_BAD=no
   ACVERSION=
   ACLOCAL=aclocal${ACVERSION}
   AUTOCONF=autoconf
   AUTOHEADER=autoheader
   AUTOMAKE=automake
   LIBTOOLIZE=libtoolize

# Exit if a simple command exits with a non-zero status.

   set -e

# After expanding a simple command, display PS4 and the command with its
# expanded arguments.  This setting makes any error messages too
# difficult to read:
#
# set -x

# Check Autoconf version and perform clean ups if all is well.

   if [ -x `which autoconf` ] ; then

      AC_VER=`autoconf --version | head -1 | sed 's/^[^0-9]*//'`
      AC_VER_MAJOR=`echo $AC_VER | cut -f1 -d'.'`
      AC_VER_MINOR=`echo $AC_VER | cut -f2 -d'.' | sed 's/[^0-9]*$//'`

      if [ "$AC_VER_MAJOR" -lt "2" ] ; then

         echo
         echo "Autoconf 2.13 or greater may be needed to build configure."
         echo "Edit the bootstrap file to ignore this test, if desired."
         echo
         exit $EXIT_ERROR_AUTOCONF_VERSION

      fi

      if [ "$AC_VER_MINOR" -lt "13" ] ; then

         echo
         echo "Autoconf 2.13 or greater may be needed to build configure."
         echo "Edit the bootstrap file to ignore this test, if desired."
         echo
         exit $EXIT_ERROR_AUTOCONF_VERSION_2

      fi

      if [ "$AC_VER_MINOR" -lt "50" ] ; then

         if [ -e configure.ac ] ; then
            if [ ! -e configure.in ] ; then
               ln -s configure.ac configure.in
            fi
         fi
         echo "Some warnings about cross-compiling are normal."

      else

         rm -f configure.in
         if [ -x configure ] ; then

            echo The XPC configure script already exists.  Replacing it.

         fi
      fi

   else

      cat << E_O_F

   The GNU autoconf application was not found.  This project requires GNU
   autoconf (and automake, autopoint, and ac-autoconf-archive) in order to
   bootstrap itself.

E_O_F
      exit $EXIT_ERROR_AUTOCONF_VERSION_3
   fi

# Check for automake.

   amvers="none"
   if automake-1.8 --version >/dev/null 2>&1; then
      amvers="-1.8"

     # If we also have 1.6 (>> 1.6.1), use it instead because it is faster

      if automake-1.6 --version >/dev/null 2>&1; then
         if expr "`automake-1.6 --version | sed -e '1s/[^0-9]*//' -e q`" ">" "1.6.1" > /dev/null 2>&1; then
            amvers="-1.6"
         fi
      fi
   elif automake-1.7 --version >/dev/null 2>&1; then
      amvers="-1.7"

      # If we also have 1.6 (>> 1.6.1), use it instead because it is faster

      if automake-1.6 --version >/dev/null 2>&1; then
         if expr "`automake-1.6 --version | sed -e '1s/[^0-9]*//' -e q`" ">" "1.6.1" > /dev/null 2>&1; then
            amvers="-1.6"
         fi
      fi

   elif automake-1.6 --version >/dev/null 2>&1; then

      amvers="-1.6"
      if expr "`automake-1.6 --version | sed -e '1s/[^0-9]*//' -e q`" "<=" "1.6.1" > /dev/null 2>&1; then
         AUTOMAKE_BAD=yes
      fi

   elif automake-1.5 --version >/dev/null 2>&1; then

      INSTALL_BAD=yes
      amvers="-1.5"

   elif automake --version > /dev/null 2>&1; then

      amvers=""
      case "`automake --version | sed -e '1s/[^0-9]*//' -e q`" in
         0|0.*|1|1.[01234]|1.[01234][-.]*)
            amvers="none" ;;
         1.5|1.5.*)
            INSTALL_BAD=yes ;;
         1.6|1.6.0|1.6.1)
            AUTOMAKE_BAD=yes ;;
      esac
   fi

#******************************************************************************
# Check for the installation of the GNU gettext facility.
# Autopoint is available from 0.11.3, but we need at least 0.11.5
#------------------------------------------------------------------------------

# Check for pkg-config

   if pkg-config --version >/dev/null 2>&1; then
      PKGCONFIG=yes
   else
      PKGCONFIG=no
   fi

#************************************************************************
# Create config and m4 directories.  Note that they might be empty for
# this project.  Also create an include directory, mainly for config.h
# stuff.
#------------------------------------------------------------------------

   mkdir -p aux-files
   mkdir -p config
   mkdir -p m4
   mkdir -p po
   mkdir -p include

# Unlike Debian, Gentoo Linux does not automatically install a range of
# versions of the autotools, so a non-existent version of aclocal gets
# called when these values are defined.  I don't think these are necessary
# anymore, even on Debian.
#
# export WANT_AUTOCONF_2_5="1"
# export WANT_AUTOMAKE="1.6"

#************************************************************************
# Call a number of "auto" programs in the strict order shown below.  See
# "info autopoint" for details.  Note:  Earlier versions of auto-tools
# don't ignore duplicate definitions of macros.  (The system normally
# provides m4 macros in /usr/share/aclocal, but the project also
# provides them in the project's m4 directory.)
#------------------------------------------------------------------------

   if [ ${OLDCONF} != "yes" ] ; then

# Moved to $SCRIPTS_DIR/xpc-bootstrap-po, but we still need to make
# aux-files/config.rpath and some other files available, since they are
# listed in configure.ac:

      if [ $DOINTLBOOT == "yes" ] ; then
         run_cmd ${AUTOPOINT} --force
      else
         cp contrib/bin/config.rpath aux-files
         cp contrib/po/LINGUAS po
         cp contrib/po/Makefile.in.in po
         cp contrib/po/Makevars po
         cp contrib/po/Makevars.template po
         cp contrib/po/es-sample-po.txt po/es.po
         cp contrib/po/POTFILES.in po
         cp contrib/po/Rules-quot po
         cp contrib/po/*.header po
         cp contrib/po/*.sed po
         cp contrib/po/*.sin po
      fi

      run_cmd ${ACLOCAL} -I ${M4DIR} --install
      run_cmd ${AUTOCONF}
      run_cmd ${AUTOHEADER}

#  The LT_INIT macro of libtool 2.0 (formerly called AC_PROG_LIBTOOL)
#  would do this, but Debian ships with version 1.5 libtool, so we have
#  to do things the old-fashioned way.

      run_cmd ${LIBTOOLIZE} --automake --force --copy

   else
      run_cmd ${ACLOCAL} -I ${M4DIR} --install
      run_cmd ${AUTOCONF} -A ${M4DIR}
      run_cmd ${AUTOHEADER} -A ${M4DIR}
   fi

   run_cmd ${AUTOMAKE} --foreign --add-missing --copy

# Automake seems to need this one, but doesn't provide it!

   cp contrib/bin/mkinstalldirs-1.10 aux-files/mkinstalldirs

# At this point, remove files which always need to be regenerated.
# Right now, this is done with the --clean option.

# Tell the user about gettext, pkg-config and sed.

   case "${GETTEXT}" in

     yes) ;;
     no) cat << E_O_F

   =============================================================================
   NOTE: gettext is not installed on your system. The XPC build will work,
      but without internationalization support.
E_O_F
     ;;

     old) cat << E_O_F

   =============================================================================
   NOTE: An old version of gettext is installed on your system. The XPC
      build will work, but if libintl is not installed, there will be no
      internationalization support.  Upgrade to gettext 0.11.5 or later.
E_O_F
     ;;

   esac

   case "$PKGCONFIG" in

     yes) ;;
     no) cat << E_O_F

   =============================================================================
   NOTE: The "pkg-config" utility is not installed on your system; detection of
      the gtk-2.0 and GNOME 2.0 libraries will not be reliable.
E_O_F
     ;;

   esac

   case "$AUTOMAKE_BAD" in
     no) ;;
     yes) cat << E_O_F

   =============================================================================
   NOTE: Your version of automake has a bug which prevents proper plugin
      compilation. Either compile XPC with the --disable-plugins flag, or
      use a version of automake newer than 1.6.1 (1.6.2 is OK, and so are
      the 1.5 series).
E_O_F
     ;;

   esac

   case "$INSTALL_BAD" in

     no) ;;
     yes) cat << E_O_F

   =============================================================================
   NOTE: Your version of automake has a bug which prevents proper installation.
      Do not use "make install" with this version of automake, or use a
      version of automake newer than 1.5 (such as 1.6 or 1.7).
E_O_F
     ;;

   esac

   if [ -x /usr/bin/dot ] ; then
      echo "graphviz package found"
   else
      echo "! To build the documentation, you need to install graphviz."
   fi

   if [ -x /usr/bin/doxygen ] ; then
      echo "doxygen package found"
   else
      echo "! To build the documentation, you need to install doxygen."
   fi

   echo "Bootstrap complete at `date`" >> bootstrap.stamp

#************************************************************************
# --intl-boot
#------------------------------------------------------------------------

   if [ $DOINTLBOOT == "yes" ] ; then
      $SCRIPTS_DIR/xpc-bootstrap-po --boot
   fi

#************************************************************************
# --configure
#------------------------------------------------------------------------

   if [ $DOCONFIGURE == "yes" ] ; then

      DEBUGFLAG=""
      BUILDFLAG=""

      if [ $DODEBUG == "yes" ] ; then
         DEBUGFLAG="--enable-debug --disable-shared"
      fi

      echo "Running the new configure script in preparation for building."
      echo "Command:  ./configure $DEBUGFLAG $BUILDFLAG"

      if [ $DOLOG == "yes" ] ; then
         echo "$ ./configure $DEBUGFLAG $BUILDFLAG" >> $LOGFILENAME
      fi

      ./configure $DEBUGFLAG $BUILDFLAG

   else

      if [ "$DOBOOTSTRAP" == "yes" ] ; then

         cat << E_O_F

Now run './configure' to configure XPC for compilation, or './configure --help
for configuration options.  Another option is the 'build' script;
'./scripts/build --test' is handy.  Run '$SCRIPTS_DIR/build --help' for
more information.
E_O_F
      fi
   fi

   if [ ! -f po/es.po ] && [ $DOCLEAN == "no" ] && [ $DODEBCLEAN == "no" ] ; then

         cat << E_O_F

Warning: XPC Basic has not yet had internationalization set up.  You
         have either not yet run the xpc-bootstrap-po script, or you previously
         ran the xpc-bootstrap-po script with the scary --super-clean option.
         Internationalization should be set up using the --intl-boot
         option.  Run the '$SCRIPTS_DIR/xpc-bootstrap-po --help' command for
         more information.

E_O_F

   fi
fi

#******************************************************************************
# Safety backup of configure.ac and Makefile.am
#------------------------------------------------------------------------------
#
#     Because the bootstrap-po --boot process copies files from contrib/po
#     and modifies the configure.ac script, we want to make sure that we
#     back up the original versions to avoid accidentally losing changes.
#     Note the this backup is currently coincident with the --super-clean
#     operation, which is what we usually do when testing configure changes.
#
#------------------------------------------------------------------------------

if [ $DOBACKUP == "yes" ] ; then

   echo "Preserving configure.ac and Makefile.am in contrib/automake & po,"
   echo "and then creating a tarball of the whole project."
   save_config_make

fi

#******************************************************************************
# bootstrap (XPC)
#------------------------------------------------------------------------------
# Local Variables:
# End:
#------------------------------------------------------------------------------
# vim: ts=3 sw=3 et ft=sh
#------------------------------------------------------------------------------
